/*
 * Date          Author      Note
 * 2015.04.27    wzwz        密度马达设置
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "dumu_dist.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhctrlstate.h"
#include "com.h"
#include "keypane.h"
#include "info.h"
#include "sys.h"


/*
 *
 */
static void CreateDumuDistInterface(HWND hWnd)
{
    int i = 0;
    const char *sc[5] = {
        GetString(621),
        GetString(622),
        GetString(623),
        GetString(624),
        GetString(619),
    };
    for(i = 0; i < 5; ++i)
    {
        CreateWindow(MH_LABEL_NAME,
            sc[i],
            WS_CHILD | BS_LEFT | WS_VISIBLE,
            IDC_STATIC,
            20,
            60 + i * 30,
            220,
            20,
            hWnd, MH_LABEL_TEXT_GREEN | MH_LABEL_BKGND_BLACK);
    }
    char edit[5][64] = {{0}};
    sprintf(edit[0], "%u", GetDumu1DistanceToMacLeft(g_knit_data));
    sprintf(edit[1], "%u", GetDumu2DistanceToMacLeft(g_knit_data));
    sprintf(edit[2], "%u", GetDumu3DistanceToMacLeft(g_knit_data));
    sprintf(edit[3], "%u", GetDumu4DistanceToMacLeft(g_knit_data));
    sprintf(edit[4], "%d", GetDumuMaxValue(g_knit_data));
    for(i = 0; i < 5; ++i)
    {
        CreateWindow(MH_EDIT_NAME,
            edit[i],
            WS_CHILD | WS_VISIBLE,
            IDC_EDIT1 + i,
            250,
            60 + i * 30,
            60,
            20,
            hWnd, 0);
    }
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHANGE_BMP - CX_ASK_BMP - 10,
        8,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
}
/*
 * set edit value
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    uint n = atoi(sc);
    int other = FALSE;
    switch(nID)
    {
    case IDC_EDIT1:
        SetDumu1DistanceToMacLeft(g_knit_data, n);
        break;
    case IDC_EDIT1 + 1:
        SetDumu2DistanceToMacLeft(g_knit_data, n);
        break;
    case IDC_EDIT1 + 2:
        SetDumu3DistanceToMacLeft(g_knit_data, n);
        break;
    case IDC_EDIT1 + 3:
        SetDumu4DistanceToMacLeft(g_knit_data, n);
        break;
    case IDC_EDIT1 + 4:
        SetDumuMaxValue(g_knit_data, n);
        other = TRUE;
        break;
    }
    if(other)
    {
        data.dist.change.other = TRUE;
    }
    else
    {
        data.dist.change.sys = TRUE;
    }
    SetWindowAdditionalData(hWnd, data.value);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    if(nID == ID_ESC)
    {
        WND_EXTRA_DATA data;
        data.value = GetWindowAdditionalData(hWnd);
        if(data.dist.change.all)
        {
            FlushSysWndDumuMaxValue(GetHosting(hWnd), data.dist.change.sys);
        }
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
    else if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 5)
    {
        int area[5][2] = {
            {1, 1000},
            {1, 1000},
            {1, 1000},
            {1, 1000},
            {1, 1000},
        };
        int index = nID - IDC_EDIT1;
        HWND hTmp = GetDlgItem(hWnd, nID);
        RECT rect;
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        CreateKeyboardWindow(hWnd, rect, area[index][0], area[index][1], atoi(GetWindowCaption(hTmp)));
        SetWindowAdditionalData(hWnd, TRUE);
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID > IDC_EDIT1 && nID < IDC_EDIT1 + 5)
        {
            --nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 4)
        {
            ++nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
}
/*
 *
 */
static int DumuDistWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateDumuDistInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 30;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 30;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_YARN_CHANGE]);
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateDumuDistWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - CX_YARN_CHANGE_BMP) / 2;
    int y = (CY_SCREEN - CY_YARN_CHANGE_BMP) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(620);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = DumuDistWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_YARN_CHANGE_BMP;
    CreateInfo.by = y + CY_YARN_CHANGE_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1 + 4));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    else
    {
    }
    return hWnd;
}
